package cn.kgc.zuxin.service.impl;

import cn.kgc.zuxin.entity.Category;
import cn.kgc.zuxin.entity.Dish;
import cn.kgc.zuxin.entity.Setmeal;
import cn.kgc.zuxin.exception.CustomException;
import cn.kgc.zuxin.mapper.CategoryMapper;
import cn.kgc.zuxin.service.CategoryService;
import cn.kgc.zuxin.service.DishService;
import cn.kgc.zuxin.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CategoryServiceimpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
@Resource
    DishService dishService;
@Resource
    SetmealService setmealService;
    @Override
    public void remove(long ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Dish::getCategoryId,ids);
        int count = dishService.count(queryWrapper);
        if (count>0){
            throw new CustomException("当前分类下已关联菜品，不可删除！");
        }

        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Setmeal::getCategoryId,ids);
        int count1 = setmealService.count(queryWrapper1);
        if (count1>0){
            throw new CustomException("当前分类下已关联套餐，不可删除！");
        }
            super.removeById(ids);
    }

}
